home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / mc51bugs / q28519 < prev    next >
Text File  |  1988-07-29  |  1KB  |  54 lines

  1. Q28519 Incorrect Code when Initializing Int to Unsigned Character
  2. C Compiler
  3. 5.00 5.10 | 5.10
  4. MS-DOS    | OS/2
  5.  
  6. Summary:
  7.    The following program will print out failed even though the two
  8. variables k and j should be equal to each other. The problem only
  9. occurs if the expression has the following form:
  10.  
  11. int int_var1 = unsigned_char_var1 = unsigned_char_var2 % number;
  12.  
  13.  
  14.  
  15. main()
  16. {
  17.     int loop;
  18.  
  19.  
  20.     unsigned char i,j;
  21.     i = 6;
  22.     for (loop=0; loop < 1; ++loop) {
  23.            int k = j = i%3;  /* k and j should be equal */
  24.     if ( k == j)
  25.        {printf("Passed\n");
  26.         exit(0);
  27.        }
  28.     else
  29.         printf("Failed\n");
  30.         exit(1);
  31.      }
  32. }
  33.  
  34.    Microsoft has confirmed this to be a problem in Versions 5.00 and
  35. 5.10. We are researching this problem and will post new information
  36. as it becomes available.
  37.  
  38. More Information:
  39.    The compiler generates code for the byte modulus correctly; the
  40. result of this operation is in the AH register. However, the code that
  41. follows, zeros out the AH register for the conversion of the unsigned
  42. character to the signed integer. Thus, the integer variable does not
  43. get the correct value and the comparison fails.
  44.    This problem can be worked around by doing the assignment in two
  45. different steps or by casting the modulus expression to be integer,
  46. as follows:
  47.  
  48.  int k = j = (int) i%3;
  49.  
  50.  
  51.  
  52. Keywords:  buglist5.00 buglist5.10 qfbv
  53. Updated  88/07/29 14:28
  54.